package com.sdgakj.web.h5;

import com.google.common.collect.Maps;
import com.sdgakj.common.other.StringUtil;
import com.sdgakj.dto.ActivesDTO;
import com.sdgakj.entity.*;
import com.sdgakj.rest.UserLoginRestController;
import com.sdgakj.service.family.ActiveService;
import com.sdgakj.service.family.FamilyService;
import com.sdgakj.service.news.NewsService;
import com.sdgakj.service.person.PersonService;
import com.sdgakj.service.statistics.StatisticsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


@Controller
@RequestMapping(value = "/h5/find")
public class H5FindController {
	
	private static final String PAGE_SIZE = "10";

	@Autowired
	private NewsService newsService;
	@Autowired
	private StatisticsService statisticsService;
	@Autowired
	private FamilyService familyService;
	@Autowired
	private ActiveService activeService;
	@Autowired
	private PersonService personService;
	private static Map<String,String> sortTypes =Maps.newLinkedHashMap();
	private static Logger logger = LoggerFactory.getLogger(UserLoginRestController.class);

	static {
		sortTypes.put("auto", "自动");
	}

    /**
     * 发现主页
     * @return
     */
    @RequestMapping(value="/",method=RequestMethod.GET)
    public String userJsp(Long personId,Model model) {
		//Statistics statistics = statisticsService.getStatisticsById(1L);
	//	model.addAttribute("statistics", statistics);
		Long familyCount = familyService.getFamilyCount();
		model.addAttribute("familyCount", familyCount);
		Long personCount = personService.getPersonCount();
		model.addAttribute("personCount", personCount);
		List<Person> list = personService.findAll();
		int yesterday = 0;
		for (Person person : list) {
			//获取当前日期前一天
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.DATE, -1);
			String stryester = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
			//获取贴子发表日期
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String strnow = sdf.format(person.getCreatetime());
			//转成date类型
			try {
				//今天
				Date now = sdf.parse(strnow);
				//昨天
				Date yester = sdf.parse(stryester);
				int day = yester.compareTo(now);
				if (day == 0) {
					yesterday++;
				}
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		model.addAttribute("yester",yesterday);
		return "h5/find/find";
	}
	/**
	 * 云谱新闻
	 * @return
	 */
	@RequestMapping(value="/news",method=RequestMethod.GET)
	public String news(@RequestParam(value = "fId", defaultValue = "0") Long fId,@RequestParam(value = "page", defaultValue = "1") int pageNumber,
					   @RequestParam(value = "page.size", defaultValue = PAGE_SIZE) int pageSize,
					   @RequestParam(value = "sortType", defaultValue = "auto") String sortType, Model model){
		Map<String, Object> searchParams =new HashMap<>();
		Page<News> news =null;
		news = newsService.getNews(searchParams, pageNumber, pageSize, sortType);
		model.addAttribute("news",news);
		return "h5/find/news";
	}
	/**
	 *  加入家族列表展示
	 * @return
	 */
	@RequestMapping(value="/joinlist",method=RequestMethod.GET)
	public String joinList(@RequestParam(value = "userId", defaultValue = "0", required = false) Long userId,
						   @RequestParam(value = "page", defaultValue = "1") int pageNumber,
					   @RequestParam(value = "page.size", defaultValue = PAGE_SIZE) int pageSize,
					   @RequestParam(value = "sortType", defaultValue = "auto") String sortType, Model model){
		Map<String, Object> searchParams =new HashMap<>();
		Page<Family> jfamily = familyService.getfamilyjoinByPage(searchParams, pageNumber, pageSize,"", sortType);
		model.addAttribute("jfamily",jfamily);
		//通过用户id 查找到用户申请加入的家族
		if (userId!=0L) {
			Person person = personService.getPersonByUserId(userId);
			List<ActivesDTO> list = activeService.getIdByPersonId(person.getId());
			List<ActivesDTO> familyShow = new ArrayList<>();
			List<FamilyPerson> familyPersons=familyService.getFamilysByPersonId(person.getId());
			for (ActivesDTO adto:list) {
				for (FamilyPerson f:familyPersons) {
					if(f.getFfId()==adto.getFamily().getId()){
						//表示已加入的家族
						familyShow.add(adto);
					}
				}
			}
				for (ActivesDTO ac:familyShow) {
					//未加入的家族
					if(list.contains(ac)){
						list.remove(ac);
					}
			}
			model.addAttribute("list", list);
			model.addAttribute("show",familyShow);
			model.addAttribute("userId", userId);
		}
		return "h5/find/join";
	}

	/**
	 * 加入家族
	 * @return
	 */
	@RequestMapping(value="/join",method=RequestMethod.GET)
	public String join(){
		return "h5/find/join";
	}

}
